package com.bjsxt.investor.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.service.BaseServiceImpl;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.dao.IAGrantDao;
import com.bjsxt.investor.dao.IAInvestorBankDao;
import com.bjsxt.investor.dao.IAInvestorDao;
import com.bjsxt.investor.dao.IAInvestorGrantDao;
import com.bjsxt.investor.pojo.AGrant;
import com.bjsxt.investor.pojo.AInvestor;
import com.bjsxt.investor.pojo.AInvestorBank;
import com.bjsxt.investor.pojo.AInvestorGrant;
import com.bjsxt.investor.service.IInvestorService;

@Service("investorService")
public class InvestorServiceImpl extends BaseServiceImpl implements IInvestorService
{
	@Resource
	private IAGrantDao grantDao;
	@Resource
	private IAInvestorDao investorDao;
	@Resource
	private IAInvestorGrantDao investorGrantDao ;
	@Resource
	private IAInvestorBankDao investorBankDao;
	
	@Override
	public AGrant findOneGrantService(Map<String, Object> condMap)
	{
		return this.grantDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneGrantService(AGrant grant)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.grantDao.insert(grant);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", grant.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneGrantService(AGrant grant)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.grantDao.update(grant);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<AGrant> findCondListGrantService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AGrant> grantList = new ArrayList<AGrant>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			grantList = this.grantDao.findCondList(condMap);
			/* grantList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(grantList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			grantList = this.grantDao.findCondList(condMap);
		}else
		{
			//不分页查询
			grantList = this.grantDao.findCondList(condMap);
		}
		return grantList;
	}
	
	@Override
	public AInvestor findOneInvestorService(Map<String, Object> condMap)
	{
		return this.investorDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorService(AInvestor investor)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.investorDao.insert(investor);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investor.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}
	
	/**
	 * 先更新数据库,
	 * 再处理上传文件
	 * 
	 * 因为数据库支持回滚
	 * 
	 * 先处理新上传的文件
	 * 再删除老文件
	 * @throws IOException 
	 */
	@Override
	public JSONObject updateOneInvestorService(AInvestor investor,Map<String, Object> paramsMap) throws IOException
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.investorDao.update(investor);
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
			/*
			 * 真实路径:
			 * 虚拟路径(网络路径):
			 * 
			 * 共同部分
			 * */
			MultipartFile file = (MultipartFile) paramsMap.get("file");
			if(file != null && !"".equalsIgnoreCase(file.getOriginalFilename()))
			{
				/* 原来的图片路径 */
				String oldRelaPath = investor.getLogo();
				/* 文件的原始名字 */
				String originalFilename = file.getOriginalFilename() ; 
				/* 用户上传了文件 */
				/* 共同路径 */
				String relaPath = ConstatFinalUtil.BUNDLE.getString("upload.rootFoler") + "/investor/"
						+ this.dateFormatUtil.formatDate(new Date()) + "/" + UUID.randomUUID()  
						+ originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
				
				/* 真实路径  */
				String truePath = paramsMap.get("truePath") + "";
				
				/* 上传文件目标路径 */
				File targetFile = new File(truePath + relaPath);
				
				/* 目标文件夹目录得创建 */
				if(!targetFile.getParentFile().exists())
				{
					targetFile.getParentFile().mkdirs();
				}
				if(this.fileUtil.copy(file.getInputStream(), new FileOutputStream(targetFile)))
				{
					/* 赋值图片路径 */
					investor.setLogo(relaPath);
					this.investorDao.update(investor);
				}
				
				/* 删除老图片 */
				File oldFile = new File(truePath + oldRelaPath);
				if(oldFile.exists())
				{
					if(!oldFile.delete())
					{
						ConstatFinalUtil.LOGGER_SYSTEM.info("删除原来图片失败了;出资方id:{},图片路径:{}",investor.getId() , 
								oldFile.getPath());
					}
				}
			}
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<AInvestor> findCondListInvestorService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AInvestor> investorList = new ArrayList<AInvestor>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorList = this.investorDao.findCondList(condMap);
			/* investorList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorList = this.investorDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorList = this.investorDao.findCondList(condMap);
		}
		return investorList;
	}
	
	@Override
	public AInvestorGrant findOneInvestorGrantService(Map<String, Object> condMap)
	{
		return this.investorGrantDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorGrantService(AInvestorGrant investorGrant)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.investorGrantDao.insert(investorGrant);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorGrant.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorGrantService(AInvestorGrant investorGrant)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.investorGrantDao.update(investorGrant);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<AInvestorGrant> findCondListInvestorGrantService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AInvestorGrant> investorGrantList = new ArrayList<AInvestorGrant>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorGrantList = this.investorGrantDao.findCondList(condMap);
			/* investorGrantList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorGrantList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorGrantList = this.investorGrantDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorGrantList = this.investorGrantDao.findCondList(condMap);
		}
		return investorGrantList;
	}
	
	@Override
	public AInvestorBank findOneInvestorBankService(Map<String, Object> condMap)
	{
		return this.investorBankDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorBankService(AInvestorBank investorBank)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.investorBankDao.insert(investorBank);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorBank.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorBankService(AInvestorBank investorBank)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.investorBankDao.update(investorBank);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public List<AInvestorBank> findCondListInvestorBankService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AInvestorBank> investorBankList = new ArrayList<AInvestorBank>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorBankList = this.investorBankDao.findCondList(condMap);
			/* investorBankList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorBankList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorBankList = this.investorBankDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorBankList = this.investorBankDao.findCondList(condMap);
		}
		return investorBankList;
	}
}
